<template>
  <div>
    <van-icon :name="value ? 'star' : 'star'"
              :class="{ collected : value }"
              @click="iconClick"
              :loading="loading" />
  </div>
</template>

<script>
import { addCollect, deleteCollect } from '@/api/article.js'
export default {
  name: 'CollectArticle',
  props: {
    value: {
      // type: [Number, String, Object],
      required: true
    },
    articleId: {
      // type: [Number, String, Object],
      required: true
    }
  },
  data () {
    return {
      loading: false
      // value: false
    }
  },
  methods: {
    async iconClick () {
      this.loading = true
      try {
        if (this.value) {
          await deleteCollect(this.articleId)
        } else {
          await addCollect(this.articleId)
        }
        this.$emit('input', !this.value)
        this.$toast.success(!this.value ? '收藏成功' : '取消收藏')
      } catch (err) {
        this.$toast.fail('操作失败， 请重试！')
      }
      this.loading = false
    }
  }
}
</script>

<style lang="less" scoped >
.van-icon {
  margin: 0 20px;
}
.collected {
  color: #ffa500;
}
</style>
